package com.wyiot.pet.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyiot.pet.model.dto.AddressDTO;
import com.wyiot.pet.model.entity.AddressDO;
import com.wyiot.pet.model.vo.AddressVO;
import com.wyiot.core.security.UserContext;
import com.wyiot.pet.service.AddressService;
import com.wyiot.core.exception.ValidateException;
import com.wyiot.core.api.ValidateGroup;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 地址管理控制层
 *
 * @author yly
 * @since 2022/3/24
 */
@RestController
public class AddressController {

    @Resource
    AddressService addressService;

    /**
     * 通过用户找到所有该用户的地址信息
     *
     */

    @GetMapping("/address/list")
    public Page<AddressVO> queryAddressListOfUser(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum
            , @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return  addressService.queryAllAddress(pageNum , pageSize ,UserContext.getUserId());
    }

    /**
     * 保存到地址簿
     *
     * @see AddressDTO
     */
    @PostMapping("/address")
    public void saveAddress(@RequestBody @Validated({ValidateGroup.Second.class}) AddressDTO addressDTO, BindingResult result) {
        AddressDO addressDO = new AddressDO();
        BeanUtils.copyProperties(addressDTO, addressDO);
        addressDO.setAddressUserId(UserContext.getUserId());
        if (addressDTO.isAddressIsdefault()) {
            addressService.update(Wrappers.lambdaUpdate(AddressDO.class).eq(AddressDO::getAddressUserId , UserContext.getUserId()).set(AddressDO::isAddressIsdefault , false));
        }
        addressService.save(addressDO);
    }

    /**
     * 删除地址
     *
     * @param addressId 地址id
     */
    @DeleteMapping("/address/{id}")
    public void removeAddressById(@PathVariable("id") Long addressId) {
        if (!addressService.getById(addressId).getAddressUserId().equals(UserContext.getUserId()))  {
            throw ValidateException.of("非法操作");
        }
        addressService.removeById(addressId);

    }

    /**
     * 修改地址
     *
     * @see AddressDTO
     */
    @PutMapping("/address")
    public void updateAddress(@RequestBody @Validated({ValidateGroup.First.class}) AddressDTO address, BindingResult result) {
        AddressDO addressDO = new AddressDO();
        BeanUtils.copyProperties(address, addressDO);
        addressDO.setAddressUserId(UserContext.getUserId());
        if (address.isAddressIsdefault()) {
            addressService.update(Wrappers.lambdaUpdate(AddressDO.class).eq(AddressDO::getAddressUserId , UserContext.getUserId()).set(AddressDO::isAddressIsdefault , false));
        }
        addressService.updateById(addressDO);
    }

    /**
     * 把一个地址设定为默认地址
     *
     * @param addressId 地址id
     */
    @PutMapping("/address/IsDefault/{addressId}")
    public void updateAddressIsDefault(@PathVariable("addressId") Long addressId) {
        AddressDO addressDO = addressService.getById(addressId);
        if (!UserContext.getUserId().equals(addressDO.getAddressUserId())) {
            throw ValidateException.of("非法操作");
        }
        addressService.isDefault(addressId);
    }

    /**
     * 查找用户的默认地址
     */
    @GetMapping("/address/IsDefault")
    public AddressVO queryAddressIsDefault() {
        return addressService.queryDefaultAddress(UserContext.getUserId());
    }

    /**
     * 在寄件时把 点击了设为默认地址的地址保存到数据库
     *
     * @see AddressDTO
     */
    @PostMapping("/address/DefaultAddress")
    public void insertDefaultAddress(@RequestBody @Validated({ValidateGroup.Second.class}) AddressDTO addressDTO) {
        addressService.insertDefaultAddress(addressDTO);
    }

}
